package exchange.util;

import java.util.LinkedList;

import exchange.util.Exchange;
import exchange.util.Order;
import exchange.util.ParseException;
import exchange.util.Stock;

public class InfoService {
	
	public static String sendStocks() {
		StringBuilder strBldr = new StringBuilder();
		for (Stock stk : Exchange.getStocks() ){
			strBldr.append(stk.getName() + ";");
		}
		System.out.println(strBldr.toString() );
		return strBldr.toString();
	}

	public static String sendStockGlass(Stock stock) throws ParseException {
		StringBuilder strBldr = new StringBuilder();
		LinkedList<Order> bidList = stock.getBidList();
		LinkedList<Order> askList = stock.getAskList();
		
		synchronized (bidList) {
			for (Order order : bidList ){
				strBldr.append( order.getPrice() + ";" + order.getAmount() + "|");
			}
		}
		strBldr.append("=");
		synchronized (askList) {
			for (Order order : askList ){
				strBldr.append( order.getPrice() + ";" + order.getAmount() + ";");
			}
		}
		
		return strBldr.toString();
	}
	
	public static void printOrderBook(Stock stock) {
		System.out.println("OrderBook Print:\n" +
				"ASK list-----");
		LinkedList<Order> list = stock.getAskList();
		
		synchronized (list) {
			for (Order ord : list ){
				System.out.println(ord.toString() );
			}
		}
		
		list = stock.getBidList();
		System.out.println("BID list-----");
		
		synchronized (list) {
			for (Order ord : list ){
				System.out.println(ord.toString() );
			}
		}
		
		System.out.println("-----------------");
	}
}
